<template>
  <div class="member-order">
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
      ></XtxTabsPanel>
    </XtxTabs>
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        v-for="item in orderList"
        @on-cancel-order="onCancelOrder(item)"
        @on-delete="handlerDelete"
        @on-confirm="handlerConfirm"
        @on-logistics="handlerLogistics"
        :key="item.id"
        :order="item"
      />
    </div>
    <!-- 分页组件 -->
    <XtxPagination
      v-if="total > 0"
      :current-page="reqParams.page"
      :page-size="reqParams.pageSize"
      :total="total"
      @current-change="reqParams.page = $event"
    />
    <!-- 对于像：对话框，消息提示，确认提示，通知组件 适合使用传送门 Teleport -->
    <Teleport to="#dailog">
      <OrderCancel @getOrderList="getOrderList" ref="orderCancelCom" />
      <!-- 查看物流组件 -->
      <OrderLogistics ref="orderLogisticsCom" />
    </Teleport>
  </div>
</template>
<script>
import { reactive, ref, watch } from "vue";
import { orderStatus } from "@/api/constants";
import OrderItem from "./components/order-item.vue";
import { confirmOrder, deleteOrder, findOrderList } from "@/api/order";
import OrderCancel from "./components/order-cancel.vue";
import Confirm from "@/components/library/Confirm";
import Message from "@/components/library/Message";
import OrderLogistics from "./components/order-logistics.vue";
export default {
  name: "MemberOrderPage",
  setup() {
    const activeName = ref("all");
    // 筛选条件
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0,
    });
    const orderList = ref([]);
    const loading = ref(false);
    const total = ref(0);

    const getOrderList = () => {
      loading.value = true;
      findOrderList(reqParams).then((data) => {
        orderList.value = data.result.items;
        total.value = data.result.counts;
        loading.value = false;
      });
    };
    // 点击选项卡
    const tabClick = ({ index }) => {
      reqParams.page = 1;
      reqParams.orderState = index;
    };
    // 筛选条件变化重新加载
    watch(
      reqParams,
      () => {
        getOrderList();
      },
      { immediate: true }
    );
    // 删除订单
    const handlerDelete = (order) => {
      Confirm({ text: "亲，您确认删除该订单吗？" })
        .then(() => {
          deleteOrder(order.id).then(() => {
            Message({ type: "success", text: "删除成功" });
            getOrderList();
          });
        })
        .catch(() => {});
    };
    // 确认收货逻辑
    const handlerConfirm = (order) => {
      Confirm({ text: "亲，您确认收货吗？确认后货款将打给卖家。" })
        .then(() => {
          confirmOrder(order.id).then(() => {
            Message({ type: "success", text: "确认收货成功" });
            // 待收货--->待评价
            order.orderState = 4;
            getOrderList();
          });
        })
        .catch(() => {});
    };
    return {
      activeName,
      orderStatus,
      tabClick,
      orderList,
      loading,
      total,
      reqParams,
      getOrderList,
      handlerDelete,
      ...useCancelOrder(),
      handlerConfirm,
      ...useLogistics(),
    };
  },
  components: { OrderItem, OrderCancel, OrderLogistics },
};
// 封装逻辑-取消订单
export const useCancelOrder = () => {
  const orderCancelCom = ref(null);
  const onCancelOrder = (item) => {
    orderCancelCom.value.open(item);
  };
  return {
    orderCancelCom,
    onCancelOrder,
  };
};
// 查看物流逻辑
export const useLogistics = () => {
  const orderLogisticsCom = ref(null);
  const handlerLogistics = (order) => {
    orderLogisticsCom.value.open(order);
  };
  return { handlerLogistics, orderLogisticsCom };
};
</script>
<style scoped lang="less">
.member-order {
  height: 100%;
  background: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
